/************************************   

DESCRIPTION: This program creates the block beta histogram and .cdf figures.

************************************/ 
clear all
cd "K:\BE\1257\Projects\Medicare Claims\HospRetro\Drafts\Health_Outcomes\FigureCreation\PSPatients\RefereeResponse\MakeBlocktstats"

	use ./blockTable.dta,  replace

		label var beta_mHdeath  "Mortality"  
		label var beta_mAdeath  "Mortality"  
		label var beta_mDdeath  "Mortality"  

		label var beta_fHdeath  "Mortality"  
		label var beta_fAdeath  "Mortality"  
		label var beta_fDdeath  "Mortality"  

		label var beta_fHacutei "Acute cardiac"
		label var beta_fHami    "AMI"  
		label var beta_fHihd    "Ischemic heart"  

		label var beta_fDdiabcomp1 "Asymptomatic" 
		label var beta_fDdiabcomp2 "Symptomatic" 
		label var beta_fDglaucoma "Glaucoma"

	sort block

			foreach v of varlist share* {
				 di "`v'"
				 local i = subinstr("`v'","share","",.)
				 replace `v'=0 if `v'==.
				 gen cum`i'=`v' if _n==1
				 replace cum`i'=cum`i'[_n-1]+`v' if _n>1
				 gen flag`i'= cum`i'[_n-1]>.999 
			 }

	 graph set window fontface "Times New Roman"
	 graph set ps fontface "Times New Roman"
	 
	 rename block Block

	*Histogram Mortality 
	twoway kdensity beta_mAdeath || kdensity beta_fAdeath, title("Full sample men and women") legend(order(1 "Men" 2 "Women" ))  graphregion(color("white"))  xtitle("")   ylabel(none)  
	 graph export "./BlockHistogram_mortality.ps", as(ps) orientation(landscape) lmargin(.05) tmargin(.2) mag(200) logo(off) replace
		 
	*Histogram Hypertension
	twoway kdensity beta_fHacute || kdensity beta_fHami || kdensity beta_fHihd || kdensity beta_fHdeath, title("Hypertensive women") graphregion(color("white")) legend(order(1 "Acute cardiac" 2 "AMI" 3 "Ischemic heart" 4 "Mortality" )) xtitle("") ylabel(none)
	 graph export "./BlockHistogram_FHyp.ps", as(ps) orientation(landscape) lmargin(.05) tmargin(.2) mag(200) logo(off) replace
	twoway kdensity beta_mHacute || kdensity beta_mHami || kdensity beta_mHihd || kdensity beta_mHdeath, title("Hypertensive men") graphregion(color("white")) legend(order(1 "Acute cardiac" 2 "AMI" 3 "Ischemic heart" 4 "Mortality" ))  xtitle("") ylabel(none)
	 graph export "./BlockHistogram_MHyp.ps", as(ps) orientation(landscape) lmargin(.05) tmargin(.2) mag(200) logo(off) replace

	*Histogram Diabetes
	twoway kdensity beta_fDdiabcomp1 || kdensity beta_fDdiabcomp2  || kdensity beta_fDglaucoma  || kdensity beta_fDdeath, title("Diabetic women") graphregion(color("white"))  legend(order(1 "Asymptomatic" 2 "Symptomatic" 3 "Glaucoma" 4 "Mortality" )) xtitle("") ylabel(none)
	 graph export "./BlockHistogram_FDiab.ps", as(ps) orientation(landscape) lmargin(.05) tmargin(.2) mag(200) logo(off) replace
	twoway kdensity beta_mddiabcomp1 || kdensity beta_mddiabcomp2  || kdensity beta_mdglaucoma  || kdensity beta_mddeath, title("Diabetic men") graphregion(color("white")) legend(order(1 "Asymptomatic" 2 "Symptomatic" 3 "Glaucoma" 4 "Mortality" )) xtitle("") ylabel(none)
	 graph export "./BlockHistogram_MDiab.ps", as(ps) orientation(landscape) lmargin(.05) tmargin(.2) mag(200) logo(off) replace
	  
	*CDF
	twoway line cumblk_fAdeath cumblk_fHacuteicd8 cumblk_fDdiabcomp1 Block, title("Women") graphregion(color("white")) legend(order(1 "Full sample" 2 "Hypertensive" 3 "Diabetic" ))  ytitle("Cumulative treatment share")
	 graph export "./Blockcdf_women.ps", as(ps) orientation(landscape) lmargin(.05) tmargin(.2) mag(200) logo(off) replace
	twoway line cumblk_mAdeath cumblk_mHacuteicd8 cumblk_mDdiabcomp1  Block, title("Men") graphregion(color("white"))  legend(order(1 "Full sample" 2 "Hypertensive" 3 "Diabetic" )) ytitle("Cumulative treatment share")
	 graph export "./Blockcdf_men.ps", as(ps) orientation(landscape) lmargin(.05) tmargin(.2) mag(200) logo(off) replace

 
 